#include <graphics.h>
#include "settings.h"
#include "snake.h"
#include "food.h"

void Food::create_food(Snake snake)
{
	remap(snake);
	x = 0;
	y = 0;
	unsigned random_num = random(box_left);
	for (int i = 0; i <= random_num; ++i)
	{
		while (map[y / LENGTH][x / LENGTH] == true)
		{
			x += LENGTH;
			if (x >= SCREEN_WIDTH)
			{
				y += LENGTH;
				x = 0;
			}
		}
		x += LENGTH;
		if (x >= SCREEN_WIDTH)
		{
			y += LENGTH;
			x = 0;
		}
	}
}

void Food::remap(Snake snake)
{
	for (int i = 0; i < BOX_HEIGHT; ++i)
		for (int j = 0; j < BOX_WIDTH; ++j)
			map[i][j] = false;
	for (int i = 0; i < snake.body.size(); ++i)
		map[snake.body[i].y / LENGTH][snake.body[i].x / LENGTH] = true;
}

void Food::draw(void)
{
	setfillcolor(WHITE);
	bar(x, y, x + LENGTH, y + LENGTH);
}